﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;


namespace Image2d
{
    class Player:AnimatedObject2D
    {
        //
        KeyboardState teclado;
        //
        float Vel;
        public float Velocity
        {
            get { return Vel; }
        }
        //
        int VelMax;
        public float VelocityMaximun
        {
            get { return VelMax; }
        }
        //
        Vector2 direction;
        //
        Vector2 clientBounds;
        //
        public ShotManager shots;


        public Player(Texture2D image, Animation[] animations, Vector2 position,
            Texture2D shot, Animation[] shotAnimations,Animation[] specialAnimations,  Vector2 bounds)
            : base(image, position, animations)
        {
            Origin = new Vector2(Rect.Width / 2, Rect.Height / 2);
            shots = new ShotManager(shot, position, shotAnimations,specialAnimations, bounds);
            clientBounds = bounds;
            Initialize();
        }

        protected void Initialize()
        {            
            direction = new Vector2(0, 0);
            Vel = 0f;
            VelMax = 15;
        }

        public override void Update(GameTime gameTime)
        {
            teclado = Keyboard.GetState(); 

             
            //rotation update
            if (teclado.IsKeyDown(Keys.A) || teclado.IsKeyDown(Keys.Left))
            {
                Rotation -= 0.1f;
            } 
            else if (teclado.IsKeyDown(Keys.D) || teclado.IsKeyDown(Keys.Right))
            {
                Rotation += 0.1f;
            }

            //speed frount or backward
            if (teclado.IsKeyDown(Keys.W) || teclado.IsKeyDown(Keys.Up))
            {
                Vel += 0.1f;
                if (Vel > VelMax)
                    Vel = VelMax;
            }
           else if (teclado.IsKeyDown(Keys.S) || teclado.IsKeyDown(Keys.Down))
            {
                Vel -= 0.1f;               
               if (Vel < -VelMax)
                    Vel = -VelMax;
            }
            //speed decay
            else
            {
                if (Vel > 1f) Vel -= 0.1f;
                else if (Vel < -1f) Vel += 0.1f;
                else Vel = 0;

            }
             
            if (teclado.IsKeyDown(Keys.Space))
            {
                shots.AddShot(gameTime);
            }

            if (teclado.IsKeyDown(Keys.Z))
            {
                shots.AddSpecial(gameTime);
            }

            shots.Update(gameTime, this);



            direction = new Vector2((float)Math.Sin(Rotation), -(float)Math.Cos(Rotation));
            Position += Vel*direction;
            

            CheckBound();
            base.Update(gameTime);
        }
        

         //
         public void CheckBound()
         {
            //teleport ship from one side to other

           
             //se ele sair esquerda vai para direita
            if (Position.X < 0 - this.currentAnimation.frameSize.X/2 )
            {
                Position = new Vector2(clientBounds.X + this.currentAnimation.frameSize.X / 2 , Position.Y);
            }
            //se ele sair esquerda vai para direita
            if (Position.X > clientBounds.X + this.currentAnimation.frameSize.X / 2)
            {
                Position = new Vector2(0 - this.currentAnimation.frameSize.X / 2, Position.Y);
            } 
            //cima vai para baixo
            if (Position.Y < 0 - this.currentAnimation.frameSize.Y / 2)
            {
                Position = new Vector2(Position.X, clientBounds.Y + this.currentAnimation.frameSize.Y / 2);
            } 
            //baixo vai para cima
            if (Position.Y > clientBounds.Y + this.currentAnimation.frameSize.Y / 2)
            {
                Position = new Vector2(Position.X, 0 - this.currentAnimation.frameSize.Y / 2);
            } 
         }

         public override void Draw(SpriteBatch spriteBatch, GameTime gameTime)
         {
             shots.Draw(spriteBatch, gameTime);
             base.Draw(spriteBatch, gameTime);
         }
    }
}